﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.PRM.AppCode.Lib;

namespace mojoPortal.Web.RS.PRM.Controls
{
    public partial class PropertyLeaseControl : RSBaseUserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public bool LoadPropertyLeases(int companyid, int propertyid)
        {
            bool bHasValue = false;
            hfPropertyID.Value = propertyid.ToString();
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            String sqlSELECT = "EXEC rs_uspGetPropertyLeases @CompanyID, @PropertyID";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@propertyid";
            param.Value = propertyid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            gvPropertyLeases.DataSource = ds;
            gvPropertyLeases.DataBind();
            if (gvPropertyLeases.Rows.Count > 0)
            {
                bHasValue = true;
            }
            myConnection.Close();
            return bHasValue;
        }
        protected void btnCreateNewLease_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int propertyid = int.Parse(hfPropertyID.Value);
            ucModalCreateLeaseControl.ShowModal(userIdentity.RSCompanyId, propertyid, 0);
        }
        protected void gvPropertyLeases_SelectedIndexChanged(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int propertyid = int.Parse(hfPropertyID.Value);
            int leaseid = int.Parse(gvPropertyLeases.SelectedValue.ToString());
            ucTenantControl.Visible = true;
            ucPropertyActivityHistoryControl.Visible = true;
            ucTenantControl.LoadPropertyTenants(userIdentity.RSCompanyId, propertyid, leaseid);
            ucPropertyActivityHistoryControl.LoadPropertyActivityHistory(userIdentity.RSCompanyId, propertyid, leaseid);
        }

    }
}